<?php
header("content-type:text/html;charset=utf8");
$pdo = new PDO("mysql:host=127.0.0.1;dbname=07c;charset=utf8","root","root");
$city = $_POST['city'];
$selectsql = "select * from wea where city= '$city'";
$res = $pdo ->query($selectsql)->fetchAll(PDO::FETCH_ASSOC);
//print_r($res);die;
if(!$res)
{
    $url ="http://api.weather.com/ser.php?city=$city";
    $data = file_get_contents($url);
    $data = json_decode($data,true);
//    print_r($data);die;
    $res = $data['result'];
    foreach ($res as $v)
    {
          $week = $v['week'];
          $city = $v['citynm'];
          $temp_high = $v['temp_high'];
          $temp_low = $v['temp_low'];
          $temperature = $v['temperature'];
          $sql = "insert into wea values ('','$city','$week','$temp_high','$temp_low','$temperature')";
          $pdo->exec($sql);
    }
}
echo json_encode($res);
?>